package tech.lixinlei.current.optional.map;

import tech.lixinlei.current.optional.map.bo.Insurance;

import java.util.Optional;

public class OptionalMapDemo {

    public static void main(String[] args) {
//        negative();
        positive();
    }

    private static void negative() {
        Insurance insurance = null;
        Optional<Insurance> optInsurance = Optional.ofNullable(insurance);
        Optional<String> s = optInsurance.map(Insurance::getName);
        System.out.println(s); // Optional.empty
        System.out.println(s.get()); // NoSuchElementException
    }

    private static void positive() {
        Insurance insurance = new Insurance("平安");
        Optional<Insurance> optInsurance = Optional.ofNullable(insurance);
        Optional<String> s = optInsurance.map(Insurance::getName);
        System.out.println(s.get()); // 平安

        Insurance insurance1 = new Insurance(null);
        Optional<Insurance> optInsurance1 = Optional.ofNullable(insurance1);
        Optional<String> s1 = optInsurance1.map(Insurance::getName);
        System.out.println(s1.get()); // NoSuchElementException
    }

}
